<?php
namespace app\admin\controller;

use app\model\AuthGroup;
use app\admin\service\MenuService;
use app\admin\service\RbacService;
use app\common\AppController;
use think\facade\Session;

/**
 * 主要用于继承系统基类
 * Class BaseController
 * @package app\admin\controller
 */
class CommonController extends AppController
{
    protected $admin_id;            //当前登录的管理员ID
    protected $admin;               //当前登录的管理员信息
    protected $fourth_menu;         //当前的菜单信息
    protected $request;             //参数接受

    public function __construct()
    {
        $this->request = request()->param();
        $this->admin_id = Session::get("admin_id");
        $this->admin = Session::get("admin");

        // 检查是否登录
        $this->checkLogin();

        // 当前控制器和方法
        $curUrl = request()->controller() . '/' . request()->method();

        // 用户的分组信息
        $group = AuthGroup::getAdminGroup($this->admin_id);

        // 检查是否有访问权限
        if (!RbacService::checkAuth($curUrl, $this->admin, $group))
        {
            $this->error(1, '暂无访问权限');
        }
    }

    /**
     * @desc 验证基础登录
     */
    private function checkLogin()
    {
        if (empty($this->admin_id)) {
            $this->redirect('/admin/login/index');
        }
    }
}
